<?php
 
	function smarty_block_historyPurchases($params, $content, &$smarty, &$repeat) {

		global $db, $config, $counter;

		$l = count($smarty->_tag_stack);
		
		
		if(empty($params['name'])) $params['name'] = "historyPurchases";

		if(empty($smarty->blockvars[$l])) {
			$sqlstr = "select a.*,b.pic from {$config->db_prefix}purchases as a left join haiou_product_sku as b on a.productid=b.id where a.type=0 and a.timestocked>0";
			if (!empty($params['productid'])) $sqlstr .= " and a.productid = ".$db->qstr($params['productid']);
			if (!empty($params['shopid'])) $sqlstr .= " and a.shopid = ".$db->qstr($params['shopid']);
			if (!empty($params['sdate'])) $sqlstr .= " and a.timestocked>=". strtotime($params['sdate']);
			if (!empty($params['edate'])) $sqlstr .= " and a.timestocked<=". (strtotime($params['edate'])+86399);
			if (!empty($params['sprice'])) $sqlstr .= " and a.realpricebuy>=". $db->qstr($params['sprice']);
			if (!empty($params['mprice'])) $sqlstr .= " and a.realpricebuy<=". $db->qstr($params['mprice']);
			
			if (!empty($params['supplierid'])) $sqlstr .= " and a.supplierid = ".$db->qstr($params['supplierid']);
			
			if (!empty($params['counter'])){
				$counter = $db->getrow(str_replace("a.*", "sum(number) number, sum(pricebuy*number) price", $sqlstr));
				$counter['avgprice'] = $counter['number']==0?0:round($counter['price']/$counter['number'],2);
				$realcounter = $db->getrow(str_replace("a.*", "sum(number) number, sum(realpricebuy*number) price", $sqlstr));
				$realcounter['avgprice'] = $realcounter['number']==0?0:round($realcounter['price']/$realcounter['number'],2);
				$smarty->assign('counter',$counter);
				$smarty->assign('realcounter',$realcounter);
			}

			if(!empty($params['priceorder']) || !empty($params['timeorder'])){
				$sqlstr .= " order by";
				if($params['priceorder']){
					$sqlstr .= " realpricebuy ".$params['priceorder'];
				}
				if($params['timeorder']){
					$sqlstr .= " timestocked ".$params['timeorder'];
				}
			}else{
				$sqlstr .= " order by timestocked desc";
			}
			
			$limit = empty($params['limit'])?9999:$params['limit'];
			$page = empty($params['page'])?(empty($_GET['list_page'])?1:$_GET['list_page']):$params['page'];
			$pager = new pager($limit, $page);
			
// 			echo $sqlstr;
			$smarty->blockvars[$l] = $pager->execute($db, $sqlstr);
			//取sku第一张图
			foreach ($smarty->blockvars[$l] as $kk=>$vv)
			{
				if(!($smarty->blockvars[$l][$kk]['product_pic'] = @strstr($vv['pic'], ",",true)))
				{
					$smarty->blockvars[$l][$kk]['product_pic'] = $vv['pic'];
				}
			}
// 			print_r($smarty->blockvars[$l]);exit;
			if(!$smarty->blockvars[$l]) return $repeat = false;
			if(!empty($params['pager'])) $smarty->assign($params['pager'], $pager);
		}
		if(list($key, $item) = each($smarty->blockvars[$l])) {
			
			$item['type']=$config->product_type[$item['type']];
			$item['usedby']=intval($item['usedby']);
			$repeat = true;
			$smarty->assign($params['name'], $item);
		}
		else {
			$repeat = false;
			$counter = "";
			reset($smarty->blockvars[$l]);
		}
		
		if(!is_null($content)) print $content;
	
		//
		if(!$repeat) $smarty->blockvars[$l] = array();
	}

?>